//
//  VIPBuyWebViewController.m
//  KidReading
//
//  Created by telen on 15/5/4.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "JSB_popWebViewController.h"
#import "ConstDefine.h"

@interface JSB_popWebViewController ()<PopWebViewDelegate>

@end

@implementation JSB_popWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

+ (JSB_popWebViewController *)popWebURL:(NSString *)url
{
    if (![TLReachability shareInstance].netStatus && ![TLReachability shareInstance].wifiStatus) {
        CountingActivity *countActivity = nil;
        
        CGSize actSize = CGSizeMake(220, 40);
        CGFloat toolHeight = 70.0;
        
        if (![NSLocalizedString(@"language", "") isEqualToString:@"cn"]) {
            actSize = CGSizeMake(240, 40);
        }
        if([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
            toolHeight = 50.0;
            actSize = CGSizeMake(170, 32);
            if (![NSLocalizedString(@"language", "") isEqualToString:@"cn"]) {
                actSize = CGSizeMake(193, 32);
            }
        }
        
        AppDelegate* appdelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        CGFloat originY = [UIScreen mainScreen].bounds.size.height - toolHeight - toolHeight*0.8;
        CGRect frame = CGRectMake(15, originY, actSize.width, actSize.height);
        UIView *inView = appdelegate.window;
        countActivity =  [[CountingActivity alloc] initWithFrame:frame InView:inView];
        [countActivity showMesg:NSLocalizedString(@"netUnavailable", nil) aotuDismiss:YES];
        return nil;;
    }
    JSB_popWebViewController* popVc = [[self alloc] init];
    PopWebView* webview = [[PopWebView alloc] initWithFrame:popVc.view.bounds];
    webview.delegate = popVc;
    [webview.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    [popVc.ctView addSubview:webview];
    [webview autoLayOut_FullWebView];
#if __Debug_Net
    NSLog(@"popUrl: %@",url);
#endif
    
    popVc.title = [NSString stringWithFormat:@"WebView_%@", url];
    
    //
    [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[PP_main,PP_main] needReturnValue:NO];
    return popVc;
}


#define __SepS0 @"::_0_::"
#define __SepS1 @"::_1_::"
#define __SepS2 @"::_2_::"


- (BOOL)popWebView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    [NSObject msgToClassName:@"JSDataProcessing" menthed:@"startURL:webView:currentVC:" refs:@[request.URL.absoluteString,webView,self] needReturnValue:NO];
    if ([request.URL.absoluteString hasPrefix:@"localcall::"]) {
        return NO;
    }
//    [JSDataProcessing startURL:request.URL.absoluteString webView:webView currentVC:self];
    NSString *requestString = [[request URL] absoluteString];
    NSArray *components = [requestString componentsSeparatedByString:__SepS0];
    if ([(NSString *)[components objectAtIndex:0] isEqualToString:@"localcall"]) {
        //
        if (components.count == 3) {
            NSString* url = [components objectAtIndex:2];
            if ([url hasPrefix:@"http"]) {
                [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
            }
        }
        //
        if([components count] > 1 && [(NSString *)[components objectAtIndex:1] isEqualToString:@"quitquiz"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"quitquiz" object:nil];
        }else if([components count] > 2){
            NSString* json = [components objectAtIndex:2];
            NSMutableDictionary* mdict = [NSMutableDictionary dictionaryWithCapacity:0];
            if (json) {
                NSArray* tempArr = [json componentsSeparatedByString:__SepS1];
                for (NSString* keyvalue in tempArr) {
                    NSArray* arr = [keyvalue componentsSeparatedByString:__SepS2];
                    if (arr.count == 2) {
                        [mdict setObject:[arr objectAtIndex:1] forKey:[arr objectAtIndex:0]];
                    }
                }
            }
            NSString *pid = [mdict valueForKey:@"pid"];
            if (pid.length) {
                [NSObject msgToClassName:@"TLMobStat" menthed:@"logEvent:eventLabel:" refs:@[PP_purchase,pid] needReturnValue:NO];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:[components objectAtIndex:1] object:self userInfo:mdict];
        }else if([components count] > 1){
            [[NSNotificationCenter defaultCenter] postNotificationName:[components objectAtIndex:1] object:self userInfo:nil];
        }
        return NO;
    }
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [NSObject msgToClassName:@"JSDataProcessing" menthed:@"setUinfoCallback:" refs:@[webView] needReturnValue:NO];
    //[JSDataProcessing setUinfoCallback:webView];
}

@end
